package routes

import (
	"myapi/internal/api/handlers"
	"myapi/internal/api/middlewares"
	"myapi/internal/repositories"
	"myapi/internal/services"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

func RegisterRoutes(r *gin.Engine, db *gorm.DB) {
	// 初始化中间件
	logger := middlewares.NewLogger()
	auth := middlewares.NewAuth()

	// 全局中间件
	r.Use(logger.LogRequest())

	// 健康检查
	r.GET("/health", func(c *gin.Context) {
		c.JSON(200, gin.H{"status": "ok"})
	})

	// 初始化仓库
	userRepo := repositories.NewUserRepository(db)
	// 初始化服务
	userService := services.NewUserService(userRepo)
	// 初始化处理器
	userHandler := handlers.NewUserHandler(userService)

	// 初始化仓库
	articleRepo := repositories.NewArticleRepository(db)
	// 初始化服务
	articleService := services.NewArticleService(articleRepo)
	// 初始化处理器
	articleHandler := handlers.NewArticleHandler(articleService)

	// API 路由组 v1
	v1 := r.Group("/api/v1")
	{
		// 用户相关路由
		users := v1.Group("/users")
		{
			users.POST("/", userHandler.CreateUser)
		}
		article := v1.Group("/article")
		{
			article.GET("/:id", articleHandler.Get)
			article.DELETE("/:id", articleHandler.Delete)
			article.POST("/store", auth.Authenticate(), articleHandler.Creat)
			article.POST("/edit/:id", auth.Authenticate(), articleHandler.Update)
			article.GET("/list", auth.Authenticate(), articleHandler.GetAll)
		}
	}
}
